//
//  ViewController2.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"
#import "ViewController.h"

@interface ViewController2 ()
@property (weak, nonatomic) IBOutlet UIView *colorview;
@property (weak, nonatomic) IBOutlet UITextField *redvalue;
@property (weak, nonatomic) IBOutlet UITextField *greenvalue;
@property (weak, nonatomic) IBOutlet UITextField *bluevalue;
@property (weak, nonatomic) IBOutlet UISlider *redslide;
@property (weak, nonatomic) IBOutlet UISlider *greenslide;
@property (weak, nonatomic) IBOutlet UISlider *blueslide;


@end

@implementation ViewController2
- (IBAction)colorchanged:(UISlider *)sender
{
    self.redvalue.text = [NSString stringWithFormat:@"%.1f",self.redslide.value];
    self.greenvalue.text = [NSString stringWithFormat:@"%.1f",self.greenslide.value];
    self.bluevalue.text = [NSString stringWithFormat:@"%.1f",self.blueslide.value];
    
//    NSLog(@"%f,%f,%f",self.redslide.value/255,self.greenslide.value/255,self.blueslide.value/255);
    [self setColor];
}
- (void)setColor
{
        self.colorview.backgroundColor = [UIColor colorWithRed:self.redslide.value/255 green:self.greenslide.value/255 blue:self.blueslide.value/255 alpha:1];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self updateinfo];
//
    // 2. 监听程序进入前台的通知,更新界面信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateinfo) name:UIApplicationWillEnterForegroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setColor) name:UIApplicationWillEnterForegroundNotification object:nil];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setColor];
}
- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender
{

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)updateinfo
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    self.redslide.value = [[d valueForKey:@"redslider"]floatValue];
    self.greenslide.value = [[d valueForKey:@"greenslider"]floatValue];
    self.blueslide.value = [[d valueForKey:@"blueslider"]floatValue];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
